/*
 * UIHexagon.fx
 *
 * Created on Feb 13, 2010, 11:57:30 AM
 */

package dawnland03.ui.model;

import javafx.geometry.BoundingBox;
import javafx.scene.*;
import javafx.scene.layout.Container;
import javafx.scene.layout.Resizable;
import javafx.scene.layout.Stack;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import dawnland03.data.map.model.MatrixCell;
import dawnland03.ui.model.Cell;
import dawnland03.ui.service.UIService;

/**
 * @author user
 */

public class UIHexagon extends CustomNode, Resizable, Cell{
  public var uiService : UIService;

  def prefWidth = uiService.getUIHexagonPrefWidth();
  def prefHeight = uiService.getUIHexagonPrefHeight();

  override var x;
  override var y;
  override var evenRow;
  public var matrixCell : MatrixCell on replace{
        x = matrixCell.getX();
        y = matrixCell.getY();
        evenRow = matrixCell.isEvenRow();
      };

  package var centerXToParent : Number;
  package var centerYToParent : Number;

  init {
      def evenRowShift = if (evenRow) prefWidth/2 else 0.0;
      /*centerXToParent = x * prefWidth + evenRowShift + prefWidth * 0.5;
      centerYToParent = y * prefHeight * 3/4 + prefHeight * 0.5;*/
  }


  override function getPrefWidth(height) {prefWidth}
  override function getPrefHeight(width) {prefHeight}

  override var layoutBounds = bind lazy BoundingBox { //bind lazy: if will be updated only when called
        //T0910021.2: remove bind lazy for UIHexagon.layoutBounds
      minX: 0
      minY: 0
      width: width
      height: height
  }

  override function create(): Node {
          def xStep = uiService.getXStep();
          def yStep = uiService.getYStep();
          def polygon = Polygon {
              points: [
                  xStep, 0.0, //up
                  2*xStep, yStep, //right-up
                  2*xStep, 3*yStep, //right-down
                  xStep, 4*yStep, //down
                  0.0, 3*yStep, //left-down
                  0.0, yStep //left-up
                  ]
              fill: uiService.getColor(matrixCell.getColor())
              stroke: Color.DARKSEAGREEN
              strokeWidth: 1.0
              /*onMousePressed: function(event) {
                  if(event.secondaryButtonDown){
                      println("id: {matrixCell.getId()}")
                  }

                  }*/
          }

          /*def circle = Circle {
              centerX: xStep;
              centerY: 2 * yStep;
              radius: yStep;
              stroke: Color.RED;
              strokeWidth: bind (roadLevel + 1.0);
              fill: Color.TRANSPARENT
          }*/
          def text = Text {
              content: matrixCell.getId().toString();
              fill: Color.DARKSEAGREEN;
              font: Font.font("Courier", FontWeight.BOLD, yStep)
          }

          Container{
              content:[
                  Stack{
                      content:[polygon, text]
                      }]
          }

  }

  public function getId() : Integer {
      return matrixCell.getId();
  }

  public function getCenterXToParent() : Number {
      centerXToParent = boundsInParent.minX + (boundsInParent.maxX-boundsInParent.minX)/2;
      return centerXToParent;
  }

  public function getCenterYToParent() : Number {
      centerYToParent = boundsInParent.minY + (boundsInParent.maxY-boundsInParent.minY)/2;
      return centerYToParent;
  }

}
